﻿/*
* 作者:haojielyb
* 邮箱:haojielyb@qq.com
*/

using Furion;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Sinks.Loki;
using SKIT.FlurlHttpClient.Wechat.Api.Settings;
using SqlSugar;
using StackExchange.Redis;
using System;
using System.Configuration;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Xml.Linq;

namespace FaceMqttClient
{

    /// <summary>
    /// 暂时无用
    /// </summary>
    internal class Program
    {
        static LokiCredentials credentials = new NoAuthCredentials("http://pod1:3100");

        public static void Main(string[] args)
        {

        


            System.Console.WriteLine("乐讯宝PayMQTT V2.0");
            try
            {
                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }



        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>

          
        Host.CreateDefaultBuilder(args)
                .Inject()
                .UseWindowsService()
                .ConfigureServices((hostContext, services) =>
                {
                    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1:6379");
                    services.AddSingleton<ConnectionMultiplexer>(redis);

                    var configConnection = new ConnectionConfig()
                    {
                        DbType = SqlSugar.DbType.MySql,
                        ConnectionString = App.Configuration["ConnectionStrings:mysql"],
                        IsAutoCloseConnection = true,

                        InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
                    };

                    SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
                        db =>
                        {

                            db.Aop.OnError = (ee) =>
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine(ee.Message);
                                Console.ForegroundColor = ConsoleColor.White;
                            };

                            //单例参数配置，所有上下文生效
                            db.Aop.OnLogExecuting = (sql, pars) =>
                            {
                                //Console.WriteLine(sql);//输出sql
                               // Console.WriteLine(UtilMethods.GetSqlString(DbType.MySql, sql, pars));
                            };
                        });
                    services.AddSingleton<ISqlSugarClient>(sqlSugar);


                    // 以下代码可不用编写，Furion 已实现自动注册 Worker;
                    // services.AddHostedService<Worker>();
                    services.AddRemoteRequest();
                    services.AddTaskScheduler();

                    services.AddConfigurableOptions<LxbOptions>();



                }).UseSerilogDefault(
                c =>
                {
                 
                    //  c.WriteTo.Console();
                    c.WriteTo.LokiHttp(credentials, new LogLabelProvider());
                    

                    //  c.WriteTo.File("mqttlog/log.log", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true);
                    // c.WriteTo.Seq("http://seq:5341");
                }
                );
    }
}